//
//  AgentsViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/4/5.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "AgentsViewController.h"
#import "AgentModel.h"

@interface AgentsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UIView *footerView;
@property (nonatomic,strong) UILabel *countLabel;

@property (nonatomic,assign) AgentsState state;
@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) NSDictionary *dataDictionary;

@end

//网络
@implementation AgentsViewController (NetWorking)

- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters = @{};
    [[AFAppDotNetAPIClient sharedClient] GET:@"AgentController/queryAgent.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"responseState"] integerValue]==0) {
            self.dataDictionary = responseObject;
            self.countLabel.text = [NSString stringWithFormat:@"总数%@",self.dataDictionary[@"count"]];
            self.dataArray = [NSArray yy_modelArrayWithClass:[AgentModel class] json:responseObject[@"obj"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

//UI
@implementation AgentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"代理商";
    self.state = AgentsState_address;
    [self.view addSubview:self.tableView];
    [self getData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}

#pragma mark - Event 

- (void)segCtrlValueChangeEvent:(UISegmentedControl *)sender{
    self.state = (AgentsState)sender.selectedSegmentIndex;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AgentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AgentsTableViewCell class])];
    cell.model = self.dataArray[indexPath.row];
    cell.state = self.state;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark - set & get

- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footerView;
        _tableView.tableHeaderView = self.headerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView registerClass:[AgentsTableViewCell class] forCellReuseIdentifier:NSStringFromClass([AgentsTableViewCell class])];
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 93)];
        [_headerView.layer addSublayer:({
            CALayer *layer = [CALayer layer];
            layer.backgroundColor = getColor(@"f1f2f6").CGColor;
            layer.frame = CGRectMake(0, 44, SCREEN_WIDTH, .5);
            layer;
        })];
        [_headerView.layer addSublayer:({
            CALayer *layer = [CALayer layer];
            layer.backgroundColor = getColor(@"f1f2f6").CGColor;
            layer.frame = CGRectMake(0, 88, SCREEN_WIDTH, 5);
            layer;
        })];
        _headerView.backgroundColor = [UIColor whiteColor];
        
        UIImageView *imgView = [UIImageView new];
        imgView.frame = CGRectMake(12, 12, 20, 20);
        UILabel *titleLabel = [UILabel new];
        titleLabel.frame = CGRectMake(CGRectGetMaxX(imgView.frame)+10, 0, 100, 44);
        titleLabel.font = DEF_FontSize_15;
        imgView.image = [UIImage imageNamed:@"dls"];
        titleLabel.text = @"渠道商";
        
        _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 44)];
        _countLabel.font = DEF_FontSize_12;
        _countLabel.textAlignment = NSTextAlignmentRight;
        
        [_headerView addSubview:imgView];
        [_headerView addSubview:_countLabel];
        [_headerView addSubview:titleLabel];
        
        UISegmentedControl *segCtrl = [[UISegmentedControl alloc] initWithItems:@[@"分布情况",@"注册时间",@"活跃情况"]];
        segCtrl.tintColor = getColor(@"3b3b3b");
        segCtrl.frame = CGRectMake(0, 0, SCREEN_WIDTH/1.5, 30);
        segCtrl.center = CGPointMake(SCREEN_WIDTH/2., 66);
        segCtrl.selectedSegmentIndex = 0;
        [segCtrl addTarget:self action:@selector(segCtrlValueChangeEvent:) forControlEvents:UIControlEventValueChanged];
        
        [_headerView addSubview:segCtrl];
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[];
    }
    return _dataArray;
}

- (NSDictionary *)dataDictionary{
    if (!_dataDictionary) {
        _dataDictionary = @{};
    }
    return _dataDictionary;
}

@end
